﻿using mozhi.smarterp.Finance.Basedatas;
using mozhi.smarterp.Finance.Basedatas.Suppliers;
using mozhi.smarterp.Finance.Querys;
using Mozhi.Abp.Core.Offices;
using Mozhi.Abp.Core.Offices.Excels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace mozhi.smarterp.Finance.Ieps.Export
{
    public class SupplierExportHandler : ExportHandlerBase<SupplierDto>
    {
        private ISupplierAppService _supplierAppService;

        public SupplierExportHandler(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
            _supplierAppService = _serviceProvider.GetService(typeof(ISupplierAppService)) as ISupplierAppService;
        }

        protected override async Task<List<SupplierDto>> GetDatasAsync()
        {
            var filter = _query.Filter.IsNullOrEmpty() ? new PageQuery() { PageIndex = 1, PageSize = int.MaxValue } :
                JsonConvert.DeserializeObject<PageQuery>(_query.Filter);

            var suppliers = await _supplierAppService.FindAsync(filter);

            return suppliers.Items as List<SupplierDto>;
        }

        protected override async Task<ExcelDocument> GetExcelDocumentAsync()
        {
            var excelDocument = new ExcelDocument();

            excelDocument.FileName = "供应商";
            excelDocument.FileType = FileType.XLSX;

            excelDocument.Sheets = GetExcelSheets();

            return excelDocument;

        }

        #region

        private List<ExcelSheet> GetExcelSheets()
        {
            var sheet = new ExcelSheet();
            sheet.SheetName = "供应商";

            sheet.Columns = GetExcelSheetColumns();

            if (_query.IsTemplate || _datas.Count == 0)
                return new List<ExcelSheet>() { sheet };

            sheet.Rows = new List<ExcelSheetRow>();

            foreach(var supplier in _datas)
            {
                var row = GetExcelSheetRow(supplier);

                sheet.Rows.Add(row);
            }

            return new List<ExcelSheet>() { sheet };
        }

        private List<ExcelSheetColumn> GetExcelSheetColumns()
        {
            var columns = new List<ExcelSheetColumn>
            {
                new ExcelSheetColumn()
                {
                    Caption = "编码",
                    Field = "Code",
                    IsRequired = true,
                },
                new ExcelSheetColumn()
                {
                    Caption = "名称",
                    Field = "Name",
                    IsRequired = true,
                },
                new ExcelSheetColumn()
                {
                    Caption = "应付科目",
                    Field = "PayableAccountName",
                    IsRequired = true,
                },
                new ExcelSheetColumn()
                {
                    Caption = "预付科目",
                    Field = "PrepaymentAccountName",
                    IsRequired = true,
                }
            };

            return columns;
        }

        private ExcelSheetRow GetExcelSheetRow(SupplierDto supplier)
        {
            var row = new ExcelSheetRow();

            row.Cells =
            [
                new ExcelSheetCell()
                {
                    Value = supplier.Code,
                    ValueType = CellValueType.Text
                },
                new ExcelSheetCell()
                {
                    Value = supplier.Name,
                    ValueType = CellValueType.Text
                },
                new ExcelSheetCell()
                {
                    Value = supplier.PayableAccountName,
                    ValueType = CellValueType.Text
                },
                new ExcelSheetCell()
                {
                    Value = supplier.PrepaymentAccountName,
                    ValueType = CellValueType.Text
                },
            ];

            return row;
        }
        #endregion
    }
}
